package com.woniuxy.array1;

/**
 * description: TestAaaryCopy <br>
 * date: 2022/2/1 4:53 下午 <br>
 * author: shesaifei <br>
 * version: 1.0 <br>
 */

/**
 * 测试数组的拷贝
 */
public class TestAaaryCopy {
    public static void main(String[] args) {
        //testBasicCopy2();
//        String[] str = {"帕里","ss","cc","op"};
//       removeElement(str,1);
        String[] s1 = {"aa", "bb", "cc"};
        extendRange(s1);
    }

    public static void testBasicCopy() {
        String[] s1 = {"aa", "bb", "cc", "dd", "ee"};
        String[] s2 = new String[10];
        System.arraycopy(s1, 2, s2, 6, 3);
        for (int i = 0; i < s2.length; i++) {
            System.out.println(i + "---" + s2[i]);
        }
    }

    //测试从数组中删除某个元素（本质上还是数组的拷贝）

    public static void testBasicCopy2() {
        String[] s1 = {"aa", "bb", "cc", "dd", "ee"};
        //String[] s2 = new String[5];

        System.arraycopy(s1, 3, s1, 3 - 1, s1.length - 3);
        s1[s1.length - 1] = null;

        for (int i = 0; i < s1.length; i++) {
            System.out.println(i + "---" + s1[i]);
        }
    }

    public static String[] removeElement(String[] s, int index) {
        /**
         *
         src - 源数组。（s） {"帕里","ss","cc","op"};
         srcPos – 源数组中的起始位置。index+1 "cc","op"
         dest - 目标数组。s {"帕里","ss","cc","op"};
         destPos – 目标数据中的起始位置。index 放在ss位置
         length – 要复制的数组元素的数量s.length-index-1 {"帕里","cc","op"};
         */
        System.arraycopy(s, index + 1, s, index, s.length - index - 1);
        s[s.length - 1] = null;
        for (int i = 0; i < s.length; i++) {
            System.out.println(i + "--" + s[i]);
        }
        return s;
    }

    //数组的扩容（本质上是：先定义一个更大的数组，然后将原数组内容原封不动拷贝到新的数组中）
    public static String[] extendRange(String[] s1) {
        //String[] s1 = {"aa","bb","cc"};
        String[] s2 = new String[s1.length + 10];
        System.arraycopy(s1, 0, s2, 0, s1.length);
        for (String s : s2) {
            System.out.println(s);
        }
        return s2;
    }
}
